#include "eventable_manager.h"

EventableManager* EventableManager::thisPointer;

EventableManager::EventableManager(void)
{
  this->pHead = NULL;
  EventableManager::thisPointer = this;
}


EventableManager::~EventableManager(void)
{
}


void EventableManager::FireEvent(int objectId, LPWSTR* params) {
  List *temp = pHead;
  while(temp != NULL) {
    if(temp->object->getObjectId() == objectId) {
      temp->object->DoEvent(params);
      return;
    }
    temp = temp->pNext;
  }
}


void EventableManager::FireAll(LPWSTR* params) {
  List *temp = pHead;
  while(temp != NULL) {
    temp->object->DoEvent(params);
    temp = temp->pNext;
  }  
}


void EventableManager::Threading() {
    List *temp = pHead;
  while(temp != NULL) {
    temp->object->DoEventThread();
    temp = temp->pNext;
  }
}


void EventableManager::Add(EventableObject *object) {
  if(this->pHead == NULL) {
    pHead = new List;
    this->pHead->object = object;
    this->pHead->pNext = NULL;
  } else {
    List *temp = new List;
    memcpy(temp, pHead, sizeof(List));
    pHead->object = object;
    pHead->pNext = temp;
  }
}


void EventableManager::Delete(EventableObject *object) {
  if(pHead == NULL)
    return;
  if(pHead->object == object) {
    if(pHead->pNext == NULL) {
      pHead = NULL;
      return;
    } else {
      pHead = pHead->pNext;
      return;
    }
  }
  List *pre = pHead;
  List *temp = pHead->pNext;
  while(temp != NULL) {
    if(temp->object == object) {
      pre->pNext = temp->pNext;
      pre = NULL;
      temp = NULL;
      return;
    } else {
      pre = temp;
      temp = temp->pNext;
    }
  }
}


void EventableManager::RemoveAll() {

}